package com.travel.find.findfragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.travel.R;
import com.travel.find.findadapter.BournRVAdapter;
import com.travel.find.findbean.HotBean;
import com.travel.find.findconstant.FindConstant;

import java.io.IOException;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import tools.Okhttputils;

/**
 * Created by Administrator on 2016/10/18.
 * 目的地fragment
 */
public class BournFragment extends Fragment {
    @Bind(R.id.rv_bourn)
    RecyclerView rvBourn;
    private View mView;
    //布局管理器
    private LinearLayoutManager manager;
    //网络获取bean数据
    String bournData;
    Handler mHandler=new Handler();
    //recyclerview 适配器
    private BournRVAdapter adapter;
    //组合json数据
    List<HotBean> countryListBeen;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.bournfragment, container, false);
        ButterKnife.bind(this, mView);
        //加载网络数据
        initview();
        return mView;
    }

    private void initview(){
        //布局管理器
        manager=new LinearLayoutManager(getContext());
        rvBourn.setLayoutManager(manager);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    bournData = Okhttputils.getInstance().run(FindConstant.BOURN);
                    //加载json数据
                    initHotData(bournData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //加载数据
    private void initHotData(String bournData) {
        String data = JSONObject.parseObject(bournData).getString("data");
        String countryList = JSONObject.parseObject(data).getString("countryList");
        String hot = JSONObject.parseObject(data).getString("hot");
        String visaFree = JSONObject.parseObject(data).getString("visaFree");
//        Log.i("tag", "==========="+countryList+","+hot+visaFree.toString());
        String a = "["+hot+","+visaFree+","+countryList.substring(1,countryList.length());
        countryListBeen = JSONArray.parseArray(a,HotBean.class);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                adapter=new BournRVAdapter(getContext(),countryListBeen);
                rvBourn.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
